﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using WinFormStoreControl;

namespace WinFormStore.Store
{
    public partial class FrmStore : Form
    {
        public FrmStore()
        {
            InitializeComponent();
        }

        private void FrmStore_Load(object sender, EventArgs e)
        {
            BtnType();
            AllGoods();
   
        }

        #region 循环输出类型按钮
        private void BtnType()
        {
            int buttonXPosition = 0; // 初始按钮x位置
            int margin = 5;
            DataTable dt = new DataTable();
            DBHelper.PrepareSql("select TypeName from [Type]");
            dt = DBHelper.ExecuteQuery();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
               System.Windows.Forms.Button button = new System.Windows.Forms.Button
                {
                    Name = "btn" + dt.Rows[i]["TypeName"].ToString(),
                    Text = dt.Rows[i]["TypeName"].ToString(),
                    BackColor = Color.OliveDrab,
                    ForeColor = Color.White,
                    FlatStyle = FlatStyle.Flat,
                    Font = new Font("华文琥珀", 9f),
                    Margin = new Padding(0),
                    Padding = new Padding(0),
                    Location = new Point(buttonXPosition + margin, 10),
                    Size = new Size(100, 30),
                };
                
                button.FlatAppearance.BorderSize = 0;
                panel3.Controls.Add(button);
                buttonXPosition += 100 + margin;
                button.Click += Button_Click;
            }
        }
        #endregion

        #region 点击类型按钮切换浏览的商品
        private void Button_Click(object sender, EventArgs e)
        {

            panel2.Controls.Clear();
            System.Windows.Forms.Button btn = (System.Windows.Forms.Button)sender;
            if (btn != null && !btn.Name.Equals(""))
            {
                string buttonName = btn.Name;
                if (buttonName == "btnAll")
                {
                    AllGoods();
                }
                else
                {
                    GoodsByName(buttonName);
                }
            }
        }
        #endregion

        #region 根据类型名称来查找数据
        private void GoodsByName(string buttonName)
        {
            int itemsRow = 3;
            int commodityCtrlWidth = 340;
            int commodityCtrlHeight = 210;
            int rowHeight = commodityCtrlHeight;
            int currentX = 0;
            int currentY = 0;
            int margin = 35;
            DBHelper.PrepareSql("select TypeID from [Type] where TypeName=@TypeName");
            DBHelper.SetParameter("TypeName", buttonName.Substring(3));
            DataTable typedt = DBHelper.ExecuteQuery();
            if (typedt.Rows.Count > 0)
            {
                int typeID = int.Parse(typedt.Rows[0]["TypeID"].ToString());
                DBHelper.PrepareSql("select * from Goods where TypeID=@TypeID");
                DBHelper.SetParameter("TypeID", typeID);
                DataTable goodsdt = DBHelper.ExecuteQuery();
                for (int i = 0; i < goodsdt.Rows.Count; i++)
                {
                    CommodityCtrl commodityCtrl = new CommodityCtrl
                    {
                        Count = int.Parse(goodsdt.Rows[i]["StockNum"].ToString()),
                        GoodsName = goodsdt.Rows[i]["GoodsName"].ToString(),
                        Price = double.Parse(goodsdt.Rows[i]["SalePrice"].ToString()),
                        Location = new Point(currentX + margin, currentY + margin),
                        Size = new Size(commodityCtrlWidth, commodityCtrlHeight)
                    };
                    panel2.Controls.Add(commodityCtrl);
                    currentX += commodityCtrlWidth + margin;
                    if ((i + 1) % itemsRow == 0)
                    {
                        currentX = 0;
                        currentY += rowHeight + margin;
                        if (currentY >= panel2.Height)
                        {
                            panel2.AutoScroll = true;
                        }
                        else
                        {
                            panel2.AutoScroll = false;
                        }
                    }
                }
            }
        }
        #endregion

        #region 查找所有数据
        private void AllGoods()
        {
            int itemsRow = 3;
            int commodityCtrlWidth = 340;
            int commodityCtrlHeight = 210;
            int rowHeight = commodityCtrlHeight;
            int currentX = 0;
            int currentY = 0;
            int margin = 35;
            DBHelper.PrepareSql("select * from Goods");
            DataTable dt = new DataTable();
            dt = DBHelper.ExecuteQuery();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                CommodityCtrl commodityCtrl = new CommodityCtrl
                {
                    Count = int.Parse(dt.Rows[i]["StockNum"].ToString()),
                    GoodsName = dt.Rows[i]["GoodsName"].ToString(),
                    Price = double.Parse(dt.Rows[i]["SalePrice"].ToString()),
                    Location = new Point(currentX + margin, currentY + margin),
                    Size = new Size(commodityCtrlWidth, commodityCtrlHeight)
                };
                panel2.Controls.Add(commodityCtrl);
                currentX += commodityCtrlWidth + margin;
                if ((i + 1) % itemsRow == 0)
                {
                    currentX = 0;
                    currentY += rowHeight + margin;
                    if (currentY >= panel2.Height)
                    {
                        panel2.AutoScroll = true;
                    }
                    else
                    {
                        panel2.AutoScroll = false;
                    }
                }
            }
        }
        #endregion
        private void btnSearch_Click(object sender, EventArgs e)
        {

        }



    }
}
